<template>
  <ContentWrap>
  <!-- <Dialog :title="dialogTitle" v-model="dialogVisible"> -->
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="130px"
    >
      <el-form-item label="企业名称" prop="corpName">
        <el-input v-model="formData.corpName" placeholder="请输入企业名称"  class="!w-440px" />
      </el-form-item>
      <el-form-item label="企业ID" prop="corpId">
        <el-input v-model="formData.corpId" placeholder="请输入企业ID" class="!w-440px" />
      </el-form-item>
      <el-form-item label="通讯录secret" prop="contactsSecret">
        <el-input type="password" v-model="formData.contactsSecret" placeholder="请输入通讯录secret" class="!w-440px" />
      </el-form-item>
      <!-- <el-form-item label="客户secret" prop="customersecret">
        <el-input v-model="formData.customersecret" placeholder="请输入客户secret" />
      </el-form-item>
      <el-form-item label="客户回调URL" prop="customerReturnUrl">
        <el-input v-model="formData.customerReturnUrl" placeholder="请输入客户回调URL" />
      </el-form-item>
      <el-form-item label="客户回调token" prop="customerReturnToken">
        <el-input v-model="formData.customerReturnToken" placeholder="请输入客户回调token" />
      </el-form-item>
      <el-form-item label="客户asekey" prop="customerEncodingAsekey">
        <el-input v-model="formData.customerEncodingAsekey" placeholder="请输入客户asekey" />
      </el-form-item> -->
      <el-form-item label="自建应用agentId" prop="agentId">
        <el-input v-model="formData.agentId" placeholder="请输入自建应用agentId" class="!w-440px" />
      </el-form-item>
      <el-form-item label="自建应用secret" prop="secret">
        <el-input type="password" v-model="formData.secret" placeholder="请输入自建应用secret" class="!w-440px" />
      </el-form-item>
      <el-form-item label="归属部门ID" prop="deptId">
        <el-tree-select
              v-model="formData.deptId"
              :data="deptList"
              :props="defaultProps"
              check-strictly
              node-key="id"
              placeholder="请选择归属部门"
              class="!w-440px"
        />
      </el-form-item>
      <el-form-item label="">
        <el-button @click="submitForm" type="primary">确 定</el-button>
        <el-button @click="resetForm">重 置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- </Dialog> -->
</template>
<script setup lang="ts">
import { WorkWxConfigApi, WorkWxConfigVO } from '@/api/crm/crmworkwxconfig'
import * as DeptApi from '@/api/system/dept'
import { defaultProps, handleTree } from '@/utils/tree'

/** 企业微信配置 表单 */
defineOptions({ name: 'WorkWxConfigForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  corpName: undefined,
  corpId: undefined,
  contactsSecret: undefined,
  customersecret: undefined,
  customerReturnUrl: undefined,
  customerReturnToken: undefined,
  customerEncodingAsekey: undefined,
  agentId: undefined,
  secret: undefined,
  deptId: undefined
})
const formRules = reactive({
  corpName: [{ required: true, message: '企业名称不能为空', trigger: 'blur' }],
  corpId: [{ required: true, message: '企业ID不能为空', trigger: 'blur' }],
  contactsSecret: [{ required: true, message: '通讯录secret不能为空', trigger: 'blur' }],
  customersecret: [{ required: true, message: '客户secret不能为空', trigger: 'blur' }],
  agentId: [{ required: true, message: '自建应用agent_id不能为空', trigger: 'blur' }],
  secret: [{ required: true, message: '自建应用secret不能为空', trigger: 'blur' }],
  deptId: [{ required: true, message: '选择归属部门', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
const deptList = ref<Tree[]>([]) // 树形结构


/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      const data = await WorkWxConfigApi.getWorkWxConfig(id)
      if(data){
        formData.value = data
      }
    } finally {
      formLoading.value = false
    }
  }
  deptList.value = handleTree(await DeptApi.getSimpleDeptList())
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as WorkWxConfigVO
    if (!data.id) {
      await WorkWxConfigApi.createWorkWxConfig(data)
      message.success(t('common.createSuccess'))
    } else {
      await WorkWxConfigApi.updateWorkWxConfig(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    corpName: undefined,
    corpId: undefined,
    contactsSecret: undefined,
    customersecret: undefined,
    customerReturnUrl: undefined,
    customerReturnToken: undefined,
    customerEncodingAsekey: undefined,
    agentId: undefined,
    secret: undefined,
    deptId: undefined
  }
  formRef.value?.resetFields()
}
</script>